package com.neuedu.funlifebbs.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.neuedu.funlifebbs.bean.Users;
import com.neuedu.funlifebbs.service.UsersService;
import com.neuedu.funlifebbs.utils.StringUtil;

/**
 * 
 * @author myh
 * @version 2019年8月19日上午11:01:09
 *
 */
@Controller
@RequestMapping("/admin")
public class AdminHomeController {

	@Autowired
	UsersService usersService;
	
	@RequestMapping("")
	public String center() {
		return "back/center";
	}

	@RequestMapping(value = "adlogin", method = RequestMethod.GET)
	public String adlogin() {
		return "adlogin";
	}
	
	@RequestMapping(value="adlogin",method=RequestMethod.POST)
	public String adlogin(Model model,String name,String password) {
		String errorMsg;
		if(StringUtil.isEmpty(name) || StringUtil.isEmpty(password)) {
			errorMsg = "账号密码不能为空";
		}else {
			Users user = usersService.login(name, password);
			if(user==null) {
				errorMsg = "账号或密码错误";
			}else if(user.getPermissions() == 0){
				errorMsg = "账号权限不足";
			}else{
				return "redirect:/admin";
			}
		}
		model.addAttribute("errorMsg", errorMsg);
		return "adlogin";
	}
}
